﻿Imports System.Data
Imports System.Drawing

Partial Class QuizAdmin
    Inherits System.Web.UI.Page
    'Might want to modify checkbox loading situations, along with check to see if selected answer is already correct.
    'If already correct, have it pre checked.
   
    Private quiz As Quizsln = New Quizsln()
    Protected Sub QuizAdmin_Load(sender As Object, e As EventArgs) Handles Me.Load
        ddlAdminFeatures.Width = 700
        'ddlAnswer.Width = ddlSelectFeature.Width
    End Sub
    ''' <summary>
    ''' Handles the initial dropdownlist to set up user administration features
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlAdminFeatures_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlAdminFeatures.SelectedIndexChanged
        ddlSelectFeature.Items.Clear()
        ddlAnswer.Items.Clear()
        txtTemp.Text = ""
        lblNotify.Text = ""
        txtTemp.Visible = False
        btnDelete.Visible = False
        chkCorrectAnswer.Visible = False
        btnInsert.Visible = False
        btnUpdate.Visible = False
        btnReset.Visible = False
        lblAnswer.Text = ""
        ddlAnswer.Visible = False
        If (ddlAdminFeatures.SelectedValue = "S") Then
            lblSelectFeature.Visible = False
            ddlSelectFeature.Visible = False

        ElseIf (ddlAdminFeatures.SelectedValue = "Update") Then
            populateQuestions()
        ElseIf (ddlAdminFeatures.SelectedValue = "Add") Then
            populateQuestions()
            btnInsert.Visible = True
            txtTemp.Visible = True
            lblNotify.Text = "Please type the new question to add a question or select a question to insert answers"
        ElseIf (ddlAdminFeatures.SelectedValue = "Reset") Then
            populateStudents()

        End If
    End Sub
    ''' <summary>
    ''' Populates the select feature dropdown list with student first name and last name to have their quiz attempts reset
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub populateStudents()
        ddlSelectFeature.Items.Clear()
        btnReset.Visible = True
        ddlSelectFeature.Visible = True
        lblSelectFeature.Visible = True
        lblSelectFeature.Text = "Select a Student:"
        ddlSelectFeature.Items.Add("Please select a student")
        Dim quizStudents As DataTable = New DataTable()
        quizStudents.Columns.Add("bannerId")
        quizStudents.Columns.Add("firstName")
        quizStudents.Columns.Add("lastName")
        quizStudents = quiz.getQuizUsersForReset()
        Dim student As String
        For index As Integer = 1 To quiz.getQuizUsersCount
            Dim newListItem As ListItem
            student = quizStudents.Rows(index - 1)("first_Name") & " " & quizStudents.Rows(index - 1)("last_Name")
            newListItem = New ListItem(student, quizStudents.Rows(index - 1)("banner_Id"))
            ddlSelectFeature.Items.Add(newListItem)
        Next
    End Sub

    ''' <summary>
    ''' Populates the question dropdownlist to be used for updating/inserting new questions
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub populateQuestions()
        ddlSelectFeature.Items.Clear()
        ddlSelectFeature.Visible = True
        lblSelectFeature.Visible = True
        lblSelectFeature.Text = "Select Question:"
        ddlSelectFeature.Items.Add("Please select a question")
        '  Dim questionNumber As Integer = quiz.getQuizQuestions()
        Dim questionId As DataTable = New DataTable()
        questionId.Columns.Add("Id")
        questionId = quiz.getQuizId()
        Dim question As String
        For index As Integer = 1 To quiz.getQuizQuestionsCount()
            Dim newListItem As ListItem
            question = quiz.getQuizQuestions(questionId.Rows(index - 1)("Id"))
            newListItem = New ListItem(question, questionId.Rows(index - 1)("Id"))
            ddlSelectFeature.Items.Add(newListItem)
        Next

    End Sub

    ''' <summary>
    ''' Populates the answer dropdownlist to be used for updating/inserting new answers, this dropdownlist is filled after a question is selected
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub populateAnswer()
        ddlAnswer.Items.Clear()
        txtTemp.Visible = True
        ddlAnswer.Items.Add("Please select a answer for edit")
        Dim quizAnswers As New DataTable
        quizAnswers.Columns.Add("Answer")
        quizAnswers.Columns.Add("Id")

        lblAnswer.Text = "Select Answer:"
        quizAnswers = quiz.getQuizAnswerAdmin(ddlSelectFeature.SelectedValue)

        'Populate answer DDL
        For index As Integer = 1 To quiz.getAnswerCount(ddlSelectFeature.SelectedValue)
            Dim newListItem As ListItem
            newListItem = New ListItem(quizAnswers.Rows(index - 1)("Answer").ToString(), quizAnswers.Rows(index - 1)("Answer_Id").ToString())
            ddlAnswer.Items.Add(newListItem)
        Next
        txtTemp.Text = ddlSelectFeature.SelectedItem.ToString()
        txtTemp.Visible = True
    End Sub

    Protected Sub ddlSelectFeature_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSelectFeature.SelectedIndexChanged
        ddlAnswer.Items.Clear()
        If ddlSelectFeature.SelectedIndex <> 0 Then
            If (ddlAdminFeatures.SelectedValue = "Update") Then
                populateAnswer()
                btnDelete.Visible = True
                btnUpdate.Visible = True
                ddlAnswer.Visible = True
            ElseIf (ddlAdminFeatures.SelectedValue = "Add") Then
                lblNotify.Text = "Please click insert and the checkbox below to add a new answer for the selected question"
                chkCorrectAnswer.Visible = True
            Else

            End If
           
        Else

            If (ddlAdminFeatures.SelectedValue = "Update") Then
                btnDelete.Visible = False
                btnUpdate.Visible = False
                lblAnswer.Text = ""
                ddlAnswer.Visible = True
            ElseIf (ddlAdminFeatures.SelectedValue = "Add") Then
                lblNotify.Text = "Please type the new question to add a question or select a question to insert answers"
                chkCorrectAnswer.Visible = False
            Else

            End If
           

        End If


    End Sub

    Protected Sub ddlAnswer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlAnswer.SelectedIndexChanged
        If ddlAnswer.SelectedIndex <> 0 Then
            btnDelete.Visible = True
            btnUpdate.Visible = True
            txtTemp.Visible = True
            txtTemp.Text = ddlAnswer.SelectedItem.ToString()

        Else
            btnDelete.Visible = False
            btnUpdate.Visible = False
        End If


    End Sub

    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If ddlAnswer.SelectedIndex = 0 Then

            quiz.updateQuizQuestions(ddlSelectFeature.SelectedValue, txtTemp.Text)
            populateQuestions()
            ddlAnswer.Visible = False
            txtTemp.Visible = False



        Else

            quiz.updateQuizAnswers(ddlAnswer.SelectedValue, txtTemp.Text)
            populateQuestions()
            ddlAnswer.Visible = False
            txtTemp.Visible = False
            lblAnswer.Text = ""
        End If
    End Sub

    Protected Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If ddlAnswer.SelectedIndex = 0 Then

            quiz.deleteQuizQuestions(ddlSelectFeature.SelectedValue)
            populateQuestions()
            ddlAnswer.Visible = False
            lblAnswer.Text = ""
            txtTemp.Text = ""
            txtTemp.Visible = False
            btnDelete.Visible = False
            btnUpdate.Visible = False
        Else
            quiz.deleteQuizAnswers(ddlAnswer.SelectedValue)
            populateQuestions()
            ddlAnswer.Visible = False
            lblAnswer.Text = ""
            txtTemp.Visible = False
            txtTemp.Text = ""
            btnDelete.Visible = False
            btnUpdate.Visible = False
        End If
    End Sub

    Protected Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        lblNotify.Text = ""
        If (ddlSelectFeature.SelectedIndex <> 0) Then
            quiz.deleteQuizStudentsAttempt(ddlSelectFeature.SelectedValue)
            populateStudents()
        Else
            lblNotify.Text = "Please select a student to reset"
        End If
    End Sub

    Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        If (txtTemp.Text <> "") Then

            If (ddlSelectFeature.SelectedIndex <> 0) Then
                Dim str As String = ""
                If (chkCorrectAnswer.Checked) Then
                    str = "Y"
                Else
                    str = "N"
                End If
                quiz.insertQuizAnswer(txtTemp.Text, ddlSelectFeature.SelectedValue, str.ToString())
                txtTemp.Text = ""
                populateQuestions()

            Else
                quiz.insertQuizQuestion(txtTemp.Text)
                txtTemp.Text = ""
                populateQuestions()
            End If
        Else
        lblNotify.Text = "Please insert text before clicking insert for either a question or answer!"
        End If
    End Sub

    Protected Sub btnImage_Click(sender As Object, e As ImageClickEventArgs) Handles btnImage.Click
        Response.Redirect("default.aspx")
    End Sub
End Class
